{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>06-python中元组(Tuple)<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#创建元祖\" data-toc-modified-id=\"创建元祖-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>创建元祖</a></span></li><li><span><a href=\"#访问元祖元素\" data-toc-modified-id=\"访问元祖元素-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>访问元祖元素</a></span></li><li><span><a href=\"#修改元祖\" data-toc-modified-id=\"修改元祖-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>修改元祖</a></span></li><li><span><a href=\"#删除元祖\" data-toc-modified-id=\"删除元祖-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>删除元祖</a></span></li><li><span><a href=\"#元祖运算符\" data-toc-modified-id=\"元祖运算符-5\"><span class=\"toc-item-num\">5&nbsp;&nbsp;</span>元祖运算符</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python 的元组与列表类似，不同之处在于元组的元素不能修改。\n",
    "\n",
    "元组使用小括号，列表使用方括号。\n",
    "\n",
    "元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。\n",
    "\n",
    "打个比方：元祖类似于只读文件，不能对元祖中的元素进行增删改。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建元祖"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（1）使用() 直接创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 9, 1, 0)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num = (3, 9, 1, 0)\n",
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('www.baidu.com', '测试')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "urls = (\"www.baidu.com\",\"测试\")\n",
    "urls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('hello', 19, [1, 2], ('c', 2.0))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abc = (\"hello\", 19, [1,2], ('c',2.0))\n",
    "abc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组中只包含一个元素时，需要在元素后面添加逗号，否则括号会被当作运算符使用："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup1 = (59)\n",
    "type(tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup2 = (59,)\n",
    "type(tup2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组与字符串类似，下标索引从0开始，可以进行截取，组合等。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）使用tuple()函数创建元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了使用( )创建元组外，Python 还提供了一个内置的函数 `tuple()`，用来将其它数据类型转换为元组类型。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tuple() 的语法格式如下：\n",
    "\n",
    "`tuple(data)`\n",
    "\n",
    "其中，data 表示可以转化为元组的数据，包括字符串、元组、range 对象等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('h', 'e', 'l', 'l', 'o')\n"
     ]
    }
   ],
   "source": [
    "# 将字符串转化为元祖\n",
    "tup1 = tuple('hello')\n",
    "print(tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('c++', 'python', 'java', 'go')\n"
     ]
    }
   ],
   "source": [
    "# 将列表转化为元祖\n",
    "list1 = ['c++', 'python', 'java', 'go']\n",
    "tup2 = tuple(list1)\n",
    "print(tup2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('a', 'b', 'c')\n"
     ]
    }
   ],
   "source": [
    "# 将字典转化为元祖\n",
    "dict1 = {'a':100, 'b':'yes', 'c':1.23}\n",
    "tup3 = tuple(dict1)\n",
    "print(tup3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 4, 5)\n"
     ]
    }
   ],
   "source": [
    "# 将区间转化为元祖\n",
    "range1 = range(1,6)\n",
    "tup4 = tuple(range1)\n",
    "print(tup4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "()\n"
     ]
    }
   ],
   "source": [
    "# 创建空元祖\n",
    "print(tuple())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 访问元祖元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组可以使用下标索引来访问元组中的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tup5[0] :  www\n",
      "tup5[1:3] :  ('abc', '2')\n"
     ]
    }
   ],
   "source": [
    "tup5 = ('www', 'abc', '2', '3.5')\n",
    "print(\"tup5[0] : \", tup5[0])\n",
    "print(\"tup5[1:3] : \", tup5[1:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用切片访问元组元素的格式为：\n",
    "\n",
    "`tuplename[start : end : step]`\n",
    "\n",
    "其中，start 表示起始索引，end 表示结束索引，step 表示步长。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 修改元祖"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组中的元素值是不允许修改的，但我们可以对元组进行连接组合或重新定义。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "('a', 'b', 'c')\n"
     ]
    }
   ],
   "source": [
    "# 对元祖变量重新赋值\n",
    "tup6 = (1,2,3)\n",
    "print(tup6)\n",
    "\n",
    "tup6 = ('a','b','c')\n",
    "print(tup6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外，还可以使用`+`拼接元祖,合成一个新元祖。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3, 'a', 'b', 'c')\n"
     ]
    }
   ],
   "source": [
    "tup7 = (1,2,3)\n",
    "tup8 = ('a', 'b', 'c')\n",
    "print(tup7 + tup8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 删除元祖"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元组中的元素值是不允许删除的，但我们可以使用del语句来删除整个元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('abc', 123, 11.3)\n"
     ]
    }
   ],
   "source": [
    "tup9 = ('abc', 123, 11.3)\n",
    "print(tup9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'tup9' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-1a3c8a13fbd8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdel\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtup9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtup9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'tup9' is not defined"
     ]
    }
   ],
   "source": [
    "del(tup9)\n",
    "print(tup9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 元祖运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与字符串一样，元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制，运算后会生成一个新的元组。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "| Python 表达式                  | 结果                         | 描述         |\n",
    "| :----------------------------- | :-------------------- | :----------- |\n",
    "| len((1, 2, 3))                 | 3                            | 计算元素个数 |\n",
    "| (1, 2, 3) + (4, 5, 6)          | (1, 2, 3, 4, 5, 6)           | 连接         |\n",
    "| ('Hi!',) * 4                   | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | 复制         |\n",
    "| 3 in (1, 2, 3)                 | True                         | 元素是否存在 |\n",
    "| for x in (1, 2, 3): print (x,) | 1 2 3                        | 迭代         |"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "06-python中元组(Tuple)",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
